package com.atguigu.lease.web.admin.custom.config;

import com.atguigu.lease.web.admin.custom.convert.StringToBaseEnumConverterFactory;
import com.atguigu.lease.web.admin.custom.convert.StringToItemTypeConvert;
import com.atguigu.lease.web.admin.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Value("${admin.auth.path-patterns.include}")
    private String[] includes;

    @Value("${admin.auth.path-patterns.exclude}")
    private String[] excludes;

    //todo: springmvc的组件，需要主动配置到mvc配置类中才生效
    @Override
    public void addFormatters(FormatterRegistry registry) {
        // 添加自定义转换器
//        registry.addConverter(new StringToItemTypeConvert());
        // 添加自定义转换器工厂
        registry.addConverterFactory(new StringToBaseEnumConverterFactory());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加拦截器
        registry.addInterceptor(new AuthenticationInterceptor())
                .addPathPatterns(includes)
                .excludePathPatterns(excludes).order(1);
    }
}
